package com.znzz.course.util.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;

/**
 * 数据库操作类-----生成本地所需的数据库
 * @Author by DHF
 * @Date 2022/04/2022/4/13 22:03
 * @Version 1.0
 */
public class DataBaseHelper extends SQLiteOpenHelper {
    private final String TAG = DataBaseHelper.class.getName();
    public static DataBaseHelper databaseHelper;

    public DataBaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, @Nullable int version) {
        super(context, name, factory, version);
    }

    /**
     * 获取数据库的唯一实例
     *
     * @param context 上下文
     * @return 数据库实例
     */
    public static DataBaseHelper getInstance(Context context) {
        if (databaseHelper == null) {
            databaseHelper = new DataBaseHelper(context, "database.db", null, 1);
        }
        return databaseHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建课程信息数据表
        String SQL = "create table courses(" +
                "id integer primary key autoincrement," +
                "course_name text," +
                "teacher text," +
                "class_room text," +
                "day integer," +
                "class_start integer," +
                "class_end integer,"+
                "week_start integer,"+
                "week_end integer,"+
                "remark text)";
        db.execSQL(SQL);
        //创建学生信息表
        String studentSQL = "create table student(" +
                "id integer primary key autoincrement," +
                "studentId text," +
                "name text," +
                "password text," +
                "sex text," +
                "major text," +
                "phone text," +
                "collage text," +
                "clazzId text)";
        db.execSQL(studentSQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG, "数据库及表创建成功");
    }
}

